package com.example.mscloud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.mscloud.common.entities.Payment;
import com.example.mscloud.mapper.PaymentMapper;
import com.example.mscloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class PaymentServiceImpl implements PaymentService
{
    @Resource
    private PaymentMapper payMentMapper;

    @Override
    public int create(Payment payMent){
        return payMentMapper.create(payMent);
    }

    @Override
    public Payment getByIdNormal(Long id){
        //异常
        Payment payment = null;
        payment.getSerial();

        return payMentMapper.getByPaymentId(id);
    }

    @HystrixCommand(fallbackMethod = "fallback_timeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    @Override
    public Payment getByIdTimeout(Long id) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return payMentMapper.getByPaymentId(id);
    }

    @HystrixCommand(fallbackMethod = "fallback_exception", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    @Override
    public Payment getByIdException(Long id) {
        throw new RuntimeException("yichang");
//        return payMentMapper.getByPaymentId(id);
    }

    public Payment fallback_timeout(Long id){
        Payment payment = new Payment();
        payment.setSerial("服务超时，降级处理");
        return payment;
    }

    public Payment fallback_exception(Long id){
        Payment payment = new Payment();
        payment.setSerial("服务异常，降级处理");
        return payment;
    }

    /***************************************/

    @HystrixCommand(fallbackMethod = "circuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), // 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), // 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 请求时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") // 时间窗口期内失败率达到多少
    })
    @Override
    public Payment circuitBreaker(Long id){
        Random random = new Random();
        int i = random.nextInt(10);
        System.out.println("random-->"+i);
        if (i < 5) {
            throw new RuntimeException("error");
        }
        Payment payment = new Payment();
        payment.setSerial(IdUtil.randomUUID());
        return payment;
    }

    public Payment circuitBreaker_fallback(Long id){
        Payment payment = new Payment();
        payment.setSerial("error 啦！");
        return payment;
    }

}
